package hu.vpszd.entities.customer


import hu.vpszd.entities.BaseEntity
import hu.vpszd.entities.transaction.LoanApplication
import hu.vpszd.entities.transaction.Transaction

import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.OneToMany
import javax.persistence.Table
import javax.validation.constraints.NotNull

import org.hibernate.annotations.LazyCollection
import org.hibernate.annotations.LazyCollectionOption



@Entity
@Table(name = "CheckingAccount")
class CheckingAccount extends BaseEntity {

	@Column(unique = true, length = 10)
	String bankAccountNumber

	@OneToMany(cascade = CascadeType.ALL)
	List<LoanApplication> loanApplications = [];

	@OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	List<Transaction> outgoingTransactions = [];

	@OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	List<Transaction> incomingTransactions = [];

	@NotNull
	Date openedDate = new Date()

	Integer getBalance() {
		int incoming = 0
		incomingTransactions.each {
			incoming += it.sum
		}
		int outgoing = 0
		outgoingTransactions.each {
			outgoing += it.sum
		}
		return incoming - outgoing
	}
	
}
